package com.suntop.carousel.qlmediaplay.tools;

import android.annotation.SuppressLint;
import android.content.Context;

import com.suntop.carousel.qlmediaplay.media.IMediaPlayer;
import com.suntop.carousel.qlmediaplay.video.player.AndroidMediaPlayer;
import com.suntop.carousel.qlmediaplay.video.player.IVideoPlayer;
import com.suntop.carousel.qlmediaplay.video.player.QNMediaPlayer;

import java.util.ArrayList;
import java.util.HashMap;

public class MediaPlayManager {
    private HashMap<String, IVideoPlayer> stringIMediaPlayerHashMap=new HashMap<>();
    private MediaPlayManager(){}
    private static MediaPlayManager  isStance;

    public static MediaPlayManager getIsStance() {
        if (isStance==null){
            isStance=new MediaPlayManager();
        }
        return isStance;
    }

    @SuppressLint("NewApi")
    public IVideoPlayer initMediaPlayer(String tag, int type, Context context){
        IVideoPlayer iMediaPlayer = stringIMediaPlayerHashMap.get(tag);
        if (iMediaPlayer!=null){
            if (iMediaPlayer.isOnPlaying()){
                iMediaPlayer.stop();
            }
            iMediaPlayer.reset();
            return iMediaPlayer;
        }
        if (type==1){
            iMediaPlayer=new QNMediaPlayer(context).setVideoTag(tag);
        }else {
            iMediaPlayer=new AndroidMediaPlayer(context).setVideoTag(tag);
        }
        stringIMediaPlayerHashMap.put(tag,iMediaPlayer);
        return iMediaPlayer;
    }


    public void  release(String tag){
        IVideoPlayer iVideoPlayer = stringIMediaPlayerHashMap.get(tag);
        if (iVideoPlayer!=null){
            iVideoPlayer.release();
        }
        stringIMediaPlayerHashMap.remove(tag);
    }


}
